import Vue from 'vue';
import Qs from 'qs';
import router from '../router';

export default {
  ganggangUrl: {},
  myurl: {
    login: "http://localhost/auth/oauth/token",
    getMenu: "http://localhost/auth/sys/power/queryPowersByUid",
    cityList: "http://localhost/hotel/city/queryAll",
    cityInsert: "http://localhost/hotel/city/insert",
    hotelList: "http://localhost/hotel/hotel/queryAll",
    hotelInsert: "http://localhost/hotel/hotel/insert",
    roomList: "http://localhost/hotel/room/queryAll",
    roomInsert: "http://localhost/hotel/room/insert",
    priceList: "http://localhost/hotel/room/price/queryByRid",
    priceUpdate: "http://localhost/hotel/room/price/updatePrice",
    uploadUrl: "http://localhost/hotel/file/upload",
    sysUserList: "http://localhost/auth/sys/user/queryAll",
    sysRoleList: "http://localhost/auth/sys/role/queryAll",
    sysRoleListByUid: "http://localhost/auth/sys/role/queryRolesByUid",
    sysUserUpdateRole: "http://localhost/auth/sys/role/updateUserRole",
    sysPowerList: "http://localhost/auth/sys/power/queryAll",
    sysPowerListByRoid: "http://localhost/auth/sys/power/queryPowersByRoid",
    sysPowerUpdateByRoid: "http://localhost/auth/sys/power/updatePowerByRole",
    couponTempList: "http://localhost/coupon/coupon/temp/list",
    uploadCouponTemp: "http://localhost/coupon/coupon/temp/upload",
    couponList: "http://localhost/coupon/coupon/list",
    couponInsert: "http://localhost/coupon/coupon/insert"
  },
  //尝试刷新令牌
  refreshToken: function() {
    var vt = this;
    //获得需要刷新的令牌
    var rToken = localStorage.getItem("refreshToken");
    this.myajax({
      type: "POST",
      url: this.myurl.login,
      data: {
        refresh_token: rToken,
        grant_type: "refresh_token",
        client_id: "system",
        client_secret: "system",
        scope: "all"
      },
      success: function(data){
        //获取新的令牌
        var accessToken = data.access_token;
        localStorage.setItem("accessToken", accessToken);
        //重新发送一次刚刚的请求
        vt.myajax(vt.ganggangUrl);
      }
    });
  },
  myajax: function(obj) {
    var vt = this;

    var headers = {};
    //判断当前是否存在认证令牌
    if (obj.url != this.myurl.login && localStorage.getItem("accessToken")) {
      headers.Authorization = "Bearer " + localStorage.getItem("accessToken");
    }
    //添加来源字段 1-后台系统
    headers.fromType = 1;

    Vue.axios({
      url: obj.url,
      method: obj.type ? obj.type : "GET",
      headers: headers,
      transformRequest: [function(data) {
        return Qs.stringify(data)
      }],
      data: obj.data,
      params: obj.type && obj.type == 'GET' ? obj.data : {}
    }).then(function(resp) {

      var r = resp.data;
      if (r.code && r.code == 200) {
        if (obj.success) {
          obj.success(r.data, r.page);
        }
      } else if (r.code == 400) {
        //参数校验异常
        Vue.prototype.$message.error(r.data[0]);
      } else if (r.code == 401) {
        //身份认证失败
        Vue.prototype.$message.error("用户名或者密码错误！");
      } else if (r.code == 402) {
        //缓存请求对象
        vt.ganggangUrl = obj;
        //Token令牌校验失败 -- 尝试刷新令牌
        vt.refreshToken();
      } else if (r.code == 403) {
        //令牌刷新失败
        Vue.prototype.$message.error("登录凭证失效，需要重新登录！");
        localStorage.removeItem("accessToken");
        localStorage.removeItem("refreshToken");
        router.push("/");
      } else if(r.code == 405){
        //权限不足
        Vue.prototype.$message.error("骚瑞，您的权限不够，请努力升职！");
        //router.push('/')
      } else {
        Vue.prototype.$message.error(r.msg);
      }
    }).catch(function(error) {
      if (obj.exception) {
        obj.exception(error.response);
      }
    });
  }
}
